package com.example.EventBusTest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

public class MyActivity extends Activity {

    private Person person;
    private EditText edt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        person = new Person();
        EventBus.getDefault().register(this);
        edt = (EditText) findViewById(R.id.show);
    }

    public void goTo(View view) {
        // 发送
//        EventBus.getDefault().post(new Person("MyActivity",20,21));
        EventBusTool.postSticky(new Person("MyActivity", 11, 11), "Sticky");
        Intent intent = new Intent(this, TargitActivity.class);
        startActivity(intent);
    }

    // 含有 MyActivity,当用户post事件时,只有指定了"MyActivity"的事件才会触发该函数,执行在UI线程
    @Subscriber(tag = "MyActivity")
    public void updateWidthTag(Person person) {
        String msg = "updateWidthTag=" + person.toString();
//        Log.i("fcy", msg);
        edt.setText(msg);
    }

    // 接收方法,默认的tag,执行在UI线程
    @Subscriber
    public void update(Person person){
//        Log.i("fcy", "update=" + person.toString());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
